package com.cheng.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheng.admin.constant.Constants;
import com.cheng.admin.dao.PostcardOrderMapper;
import com.cheng.admin.model.Order;
import com.cheng.admin.model.OrderProduct;
import com.cheng.admin.model.PostcardOrder;
import com.cheng.admin.service.PostcardOrderService;
import com.cheng.common.exception.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 寄明信片订单 服务实现类
 * </p>
 *
 * @author 起凡
 * @since 2022-01-17
 */
@Service(value = Constants.ORDER_TYPE_POSTCARD)
public class PostcardOrderServiceImpl extends ServiceImpl<PostcardOrderMapper, PostcardOrder> implements PostcardOrderService {
    /**
     * 计算明信片订单的价格
     *
     * @param order
     */
    @Override
    public void preprocess(Order order) {
        PostcardOrder postcardOrder = order.getPostcardOrder();
        if (StringUtils.isBlank(postcardOrder.getCover())) {
            throw new CustomException("明信片封面不能为空");
        }
        postcardOrder.setOrderId(order.getId());
        save(postcardOrder);
        order.setPriceDetail(JSON.toJSONString(postcardOrder));
    }

    @Override
    public void rollbackPreprocess(Order order) {
        remove(Wrappers.<PostcardOrder>lambdaQuery().eq(PostcardOrder::getOrderId, order.getId()));
    }
}
